package org.dromara.system.service.impl;

import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.EmailAccount;
import org.dromara.system.domain.EmailList;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.EmailTemplateBo;
import org.dromara.system.domain.vo.EmailTemplateVo;
import org.dromara.system.domain.EmailTemplate;
import org.dromara.system.mapper.EmailTemplateMapper;
import org.dromara.system.service.IEmailTemplateService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 邮件模板Service业务层处理
 *
 * @author Gavino
 * @date 2024-11-18
 */
@RequiredArgsConstructor
@Service
public class EmailTemplateServiceImpl implements IEmailTemplateService {

    private final EmailTemplateMapper baseMapper;

    /**
     * 查询启用的邮件模板
     *
     * @param agentId 代理人
     * @param type    类型
     * @param status  状态
     * @return
     */
    @Override
    public List<EmailTemplate> selectAgentTemplateList(Long agentId, Integer type, Integer status) {
        LambdaQueryWrapper<EmailTemplate> query = new LambdaQueryWrapper<>();
        query.eq(EmailTemplate::getCreateId, agentId);
        query.eq(type != null, EmailTemplate::getType, type);
        query.eq(status != null, EmailTemplate::getStatus, status);
        query.eq(EmailTemplate::getIsDelete, 0);
        return baseMapper.selectList(query);
    }

    /**
     * 查询邮件模板
     *
     * @param templateId 主键
     * @return 邮件模板
     */
    @Override
    public EmailTemplateVo queryById(Long templateId){
        return baseMapper.selectVoById(templateId);
    }

    /**
     * 分页查询邮件模板列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 邮件模板分页列表
     */
    @Override
    public TableDataInfo<EmailTemplateVo> queryPageList(EmailTemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmailTemplate> lqw = buildQueryWrapper(bo);
        Page<EmailTemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的邮件模板列表
     *
     * @param bo 查询条件
     * @return 邮件模板列表
     */
    @Override
    public List<EmailTemplateVo> queryList(EmailTemplateBo bo) {
        LambdaQueryWrapper<EmailTemplate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmailTemplate> buildQueryWrapper(EmailTemplateBo bo) {
        Map<String, Object> params = bo.getParams();
        if (LoginHelper.isService()) {
            bo.setCreateId(LoginHelper.getLoginUser().getAgentId());
            bo.setIsDelete(0);
        }
        else if(!LoginHelper.isSuperAdmin()) {
            bo.setCreateId(LoginHelper.getUserId());
            bo.setIsDelete(0);
        }

        LambdaQueryWrapper<EmailTemplate> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), EmailTemplate::getName, bo.getName());
        lqw.eq(bo.getType() != null, EmailTemplate::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getTheme()), EmailTemplate::getTheme, bo.getTheme());
        lqw.eq(StringUtils.isNotBlank(bo.getTemplate()), EmailTemplate::getTemplate, bo.getTemplate());
        lqw.eq(bo.getStatus() != null, EmailTemplate::getStatus, bo.getStatus());
        lqw.eq(bo.getIsDelete() != null, EmailTemplate::getIsDelete, bo.getIsDelete());
        lqw.eq(bo.getCreateId() != null, EmailTemplate::getCreateId, bo.getCreateId());
        lqw.like(StringUtils.isNotBlank(bo.getCreateName()), EmailTemplate::getCreateName, bo.getCreateName());
        lqw.eq(bo.getUpdateId() != null, EmailTemplate::getUpdateId, bo.getUpdateId());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateName()), EmailTemplate::getUpdateName, bo.getUpdateName());
        return lqw;
    }

    /**
     * 新增邮件模板
     *
     * @param bo 邮件模板
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EmailTemplateBo bo) {
        EmailTemplate add = MapstructUtils.convert(bo, EmailTemplate.class);
        add.setIsDelete(0);

        if (LoginHelper.isService()) {
            add.setCreateId(LoginHelper.getLoginUser().getAgentId());
            add.setCreateName(LoginHelper.getLoginUser().getAgentName());
        } else {
            add.setCreateId(LoginHelper.getUserId());
            add.setCreateName(LoginHelper.getUsername());
        }

        add.setCreateTime(DateUtils.getNowDate());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTemplateId(add.getTemplateId());
        }
        return flag;
    }

    /**
     * 修改邮件模板
     *
     * @param bo 邮件模板
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EmailTemplateBo bo) {
        EmailTemplate update = MapstructUtils.convert(bo, EmailTemplate.class);
        update.setUpdateId(LoginHelper.getUserId());
        update.setUpdateName(LoginHelper.getUsername());
        update.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 校验并批量删除邮件模板信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        LambdaQueryWrapper<EmailTemplate> query = new LambdaQueryWrapper<>();
        query.in(EmailTemplate::getTemplateId, ids);
        if (LoginHelper.isAgent()) {
            query.eq(EmailTemplate::getCreateId, LoginHelper.getUserId());
        }
        return baseMapper.delete(query) > 0;
    }
}
